using Godot;
using Godot.Collections;
using System;

public partial class UIConfirm : Node2D
{
    #region  子组件
    Panel root;
    Label message;
    Button btnCancel;
    Button btnConfirm;
    #endregion

    void GetChildComponents()
    {
        root = GetNode<Panel>("CanvasLayer/PanelRoot");
        message = GetNode<Label>("%LabelMessage");
        btnConfirm = GetNode<Button>("%ButtonSure");
        btnConfirm.Pressed += OnConfirmPressedHandler;
        btnCancel = GetNode<Button>("%ButtonCancel");
        btnCancel.Pressed += OnCancelPressedHandler;
    }

    bool isConfirmPressed = false;

    private void OnCancelPressedHandler()
    {
        isConfirmPressed = false;
        AnimateHide();
    }

    private void OnConfirmPressedHandler()
    {
        isConfirmPressed = true;
        AnimateHide();
    }


    public override void _Ready()
    {
        base._Ready();
        GetChildComponents();
    }

    Callable confirmCallable;
    Callable cancelCallable;
    Tween tween;
    Vector2 startPos = new Vector2(0, 648);
    Vector2 endPos = new Vector2(0, 0);
    public void AnimateShow(string msg, Callable ok)
    {
        confirmCallable = ok;
        // cancelCallable = cancel;
        isConfirmPressed = false;
        tween?.Stop();
        GameManager.Instance.LockInput();
        message.Text = msg;
        tween = CreateTween();
        tween.TweenProperty(root, "position", endPos, 0.2f);
    }

    public void AnimateHide()
    {
        tween?.Stop();
        tween = CreateTween();
        tween.TweenProperty(root, "position", startPos, 0.2f);
        tween.Finished += OnFinishedHhandler;
    }

    private void OnFinishedHhandler()
    {
        GameManager.Instance.AllowInput();
        if (isConfirmPressed)
        {
            confirmCallable.Call();
        }
        // else
        // {
        //     cancelCallable.Call();
        // }
    }

    public void Confirm(string msg, Callable ok)
    {
        AnimateShow(msg, ok);
    }
    public void Confirm(string msg, Callable ok, Callable cancel)
    {
        cancelCallable = cancel;
        AnimateShow(msg, ok);
    }
}
